package oocis.util;

public class TimeInter {

	Time startTime;

	Time endTime;

	public TimeInter() {
		setInter(new Time(), new Time());
	}

	public TimeInter(Time s, Time e) {
		setInter(s, e);
	}

	public TimeInter(TimeInter ts) {
		setInter(ts.startTime, ts.endTime);
	}

	/*
	 * Set the interzone time.
	 */

	public void setInter(Time s, Time e) {
		startTime = s;
		endTime = e;
	}

	/*
	 * Get the two endpoing.
	 */

	public Time getStartTime() {
		return startTime;
	}

	public Time getEndTime() {
		return endTime;
	}

	/*
	 * Is there anything wrong?
	 */

	public boolean valid() {
		return startTime.valid() && endTime.valid()
				&& startTime.lessThan(endTime);
	}

	/*
	 * Check if the time between the two endpoint.
	 */

	public boolean inInter(Time tm) {
		return startTime.lessEqual(tm) && endTime.greaterEqual(tm);
	}

	/*
	 * Add tm to two end point.
	 */

	public void goNextInter(Time tm) {
		startTime.goNextTime(tm);
		endTime.goNextTime(tm);
	}

	public TimeInter nextInter(Time tm) {
		TimeInter ti = new TimeInter(this);
		ti.goNextInter(tm);
		return ti;
	}

	/*
	 * Give the nearest time --> make the important time point.
	 */

	public Time nearestTime(Time tm) {
		if (tm.lessThan(startTime))
			return startTime;
		if (tm.greaterThan(endTime))
			return null;
		return tm;
	}

}
